//
//  MainViewController.m
//  F50
//
//  Created by 郝 巍 on 15/5/8.
//  Copyright (c) 2015年 ZACK. All rights reserved.
//

#import "MainViewController.h"
#import "MainTableViewCell.h"
#import "SettingViewController.h"
#import "ApproveViewController.h"
#import "FeedbackViewController.h"
#import "UserinfoViewController.h"
#import "MyRelationViewController.h"
#import <ShareSDK/ShareSDK.h>

@interface MainViewController ()<UIGestureRecognizerDelegate,UITableViewDelegate,UITableViewDataSource>
{
    /*
     下面数组是  图片和标题的集合
     */
    NSArray *infoArr;
    NSArray *imageArr;
 
    UITableView *_tableView;
    UILabel *content;
}
@end

@implementation MainViewController


- (void)createBackground
{
    UIView *nav_bg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 64)];
    nav_bg.backgroundColor = RGB(59, 107, 178);
    [self.view addSubview:nav_bg];
    
    //中间标题  聊天对象的名称
    
    UILabel *nav_title = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH/2 - 80, 25, 160, 30)];
    nav_title.text = @"我";
    nav_title.textAlignment = NSTextAlignmentCenter;
    nav_title.font = [UIFont boldSystemFontOfSize:20];
    nav_title.textColor = [UIColor whiteColor];
    nav_title.backgroundColor = [UIColor clearColor];
    [nav_bg addSubview:nav_title];
    
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT - 64)];
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    
    if (WIDTH > 320) {
        self.scrollView.contentSize = CGSizeMake(WIDTH, HEIGHT-64);
    }else{
        self.scrollView.contentSize = CGSizeMake(WIDTH, HEIGHT);
    }
    
    
    self.scrollView.delegate = self;
    [self.scrollView setScrollEnabled:YES];//设置pageControl
    self.scrollView.bounces = NO;
    [self.view addSubview:self.scrollView];
    
    UIImageView *bgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    bgView.image = [UIImage imageNamed:@"background"];
    bgView.userInteractionEnabled = YES;
    [self.scrollView addSubview:bgView];
    
    
    UIView *heard = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 90)];
    heard.backgroundColor = [UIColor clearColor];
    [bgView addSubview:heard];
    
    self.photoImage = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 70, 70)];
//    self.photoImage.image = [UIImage imageNamed:@"profile"];
    self.photoImage.layer.cornerRadius = 35;
    self.photoImage.layer.masksToBounds = YES;
    [heard addSubview:self.photoImage];
    
    
    self.nameLab = [[UILabel alloc]initWithFrame:CGRectMake(90, 10, 180, 30)];
    self.nameLab.font = [UIFont systemFontOfSize:22];
    self.nameLab.text = [USER_DEFAULT objectForKey:@"name"];
    self.nameLab.textColor = [UIColor whiteColor];
    [heard addSubview:self.nameLab];
    
    
    content = [[UILabel alloc]initWithFrame:CGRectMake(90, 40, WIDTH - 80 - 20, 30)];
    content.font = [UIFont systemFontOfSize:18];
    content.textColor = [UIColor whiteColor];
    [heard addSubview:content];
    
    
    UIImageView *goImage = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH - 30, 30, 20*imageW, 20*imageW)];
    goImage.image = [UIImage imageNamed:@"go"];
    [heard addSubview:goImage];
    
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap)] ;
    tap.delegate = self;
    [heard addGestureRecognizer:tap];
    
    
    for (int i = 0; i < 2; i++) {
        UILabel *line = [[UILabel alloc]initWithFrame:CGRectMake(0, 90 + i*60 , WIDTH,1)];
        line.backgroundColor =RGB(44, 86, 111);
           [bgView addSubview:line];
    }
    
    UILabel *line = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH/2, 100 , 1,40)];
    line.backgroundColor =RGB(44, 86, 111);
    [bgView addSubview:line];
    
    
    self.invite = [[UILabel alloc]initWithFrame:CGRectMake(0, 0 , WIDTH/2,30)];
    self.invite.textColor = [UIColor whiteColor];
    self.invite.textAlignment = NSTextAlignmentCenter;
    self.invite.font = [UIFont systemFontOfSize:20];
    self.invite.text = @"0";
    
    self.attention = [[UILabel alloc]initWithFrame:CGRectMake(0, 0 , WIDTH/2,30)];
    self.attention.textColor = [UIColor whiteColor];
    self.attention.textAlignment = NSTextAlignmentCenter;
    self.attention.font = [UIFont systemFontOfSize:20];
   
   
    
    for (int i = 0; i < 2; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0 + i*WIDTH/2, 90, WIDTH/2, 60);
        button.tag = 1000 +i;
        [button addTarget:self action:@selector(touchButton:) forControlEvents:UIControlEventTouchUpInside];
        [button setUserInteractionEnabled:YES];
        [bgView addSubview:button];
        
        if (i == 0) {
            [button addSubview:self.invite];
        }
        if (i==1) {
            [button addSubview:self.attention];
        }
        
        UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 30 , WIDTH/2,30)];
        lab.textColor = [UIColor whiteColor];
        lab.textAlignment = NSTextAlignmentCenter;
        lab.font = [UIFont systemFontOfSize:20];
        [lab setText:i==0?@"邀请":@"关注"];
        [button addSubview:lab];
        
    
    }
    
    
 
    
    
    
    
}
-(void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO;
    content.text = [USER_DEFAULT objectForKey:@"content"];
    
   [self.photoImage sd_setImageWithURL:[USER_DEFAULT objectForKey:@"avatar"] placeholderImage:[UIImage imageNamed:@"profile"]];
    self.attention.text = [USER_DEFAULT objectForKey:@"relations"]; // 关注数量

}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // navigationBar  状态栏
    [self createBackground];

    infoArr = [NSArray arrayWithObjects:@"投资人认证",@"投融须知",@"推荐给朋友",@"反馈建议",@"设置", nil];
    imageArr =[NSArray arrayWithObjects:@"investor",@"notice",@"share",@"feedback",@"setting", nil];

    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 150, WIDTH, HEIGHT - 64 -49) style:UITableViewStylePlain];
    
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.scrollEnabled = NO; //设置tableview 不能滚动
    _tableView.backgroundColor = RGB(35, 91, 133);
    [self.scrollView addSubview:_tableView];

}



#pragma mark ---  tableviewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 1) {
        return 3;
    }
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndentifier = @"cell";
    MainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (cell == nil) {
        cell = [[MainTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    switch (indexPath.section) {
        case 0:
            cell.chanceLab.text = infoArr[indexPath.row];
            cell.topImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageArr[indexPath.row]]];
            if (indexPath.row == 0) {
                cell.approveLab.hidden = NO;
                cell.approveLab.text = @"未认证";
            }
            break;
        case 1:
            cell.chanceLab.text = infoArr[indexPath.row + 1];
            cell.topImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageArr[indexPath.row + 1]]];
            break;
        case 2:
            cell.chanceLab.text = infoArr[indexPath.row + 4];
            cell.topImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageArr[indexPath.row + 4]]];
            break;
            
        default:
            break;
    }
   
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    switch (indexPath.section) {
        case 0:{
            ApproveViewController *approve = [[ApproveViewController alloc]init];
            [self.navigationController pushViewController:approve animated:YES];
        }
            break;
        case 1:{
            switch (indexPath.row) {
                case 1:{
                    NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"ShareSDK" ofType:@"png"];
                    
                    //构造分享内容
                    id<ISSContent> publishContent = [ShareSDK content:@"分享内容"
                                                       defaultContent:@"测试一下"
                                                                image:[ShareSDK imageWithPath:imagePath]
                                                                title:@"ShareSDK"
                                                                  url:@"http://www.mob.com"
                                                          description:@"这是一条测试信息"
                                                            mediaType:SSPublishContentMediaTypeNews];
                    //创建弹出菜单容器
                    id<ISSContainer> container = [ShareSDK container];
              
                    
                    //弹出分享菜单
                    [ShareSDK showShareActionSheet:container
                                         shareList:nil
                                           content:publishContent
                                     statusBarTips:YES
                                       authOptions:nil
                                      shareOptions:nil
                                            result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
                                                
                                                if (state == SSResponseStateSuccess)
                                                {
                                                    NSLog(NSLocalizedString(@"TEXT_ShARE_SUC", @"分享成功"));
                                                }
                                                else if (state == SSResponseStateFail)
                                                {
                                                    NSLog(NSLocalizedString(@"TEXT_ShARE_FAI", @"分享失败,错误码:%d,错误描述:%@"), [error errorCode], [error errorDescription]);
                                                }
                                            }];
                    break;
                }
                case 2:{
                    FeedbackViewController *feed = [[FeedbackViewController alloc]init];
                    [self.navigationController pushViewController:feed animated:YES];
                   
                }
                  break;
                    
                    
                default:
                    break;
            }
        }
            break;
        case 2:{
            SettingViewController *setVC = [[SettingViewController alloc]init];

            [self.navigationController pushViewController:setVC animated:YES];
        }
            break;
            
        default:
            break;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 20)];
    view.backgroundColor = RGB(12, 62, 102);
    return  view;
}

/*
 点击头像天传的界面
 */
- (void)handleTap {
    UserinfoViewController *userInfo = [[UserinfoViewController alloc]init];
    [self.navigationController pushViewController:userInfo animated:YES];
}
- (void)touchButton:(UIButton *)sender
{
    switch (sender.tag) {
        case 1000:
      
            NSLog(@"1110000011");
            break;
        case 1001:{            
            MyRelationViewController *myRelation = [[MyRelationViewController alloc]init];
            [self.navigationController pushViewController:myRelation animated:YES];
        }
            break;
        default:
            break;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
